/*
 * 
 * Copyright 2011 visual-analysis-of-cancer.
 * This file is part of visual-analysis-of-cancer.
 * 
 * visual-analysis-of-cancer is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * visual-analysis-of-cancer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with kOSMik.  If not, see <http://www.gnu.org/licenses/>.
 *  
 * visual-analysis-of-cancer:
 * An tool to analye the WHO and GLOBOCAN cancer data.
 * http://code.google.com/p/visual-analysis-of-cancer/
 * 
 * 
 * 
 * Authors: Erik Sniegula, Johannes Dahlke
 * 
 * Module:  Programming in Processing for Visual Analytics
 * Group:   20
 * 
 * Created: Aug. 2011
 * Version: 1.0
 *
 */
 




package de.fuberlin.processing.who.cancer.gui.detailgraph;

import java.util.Collection;
import java.util.Vector;

import processing.core.PApplet;

import controlP5.ControlP5;
import controlP5.ControllerGroup;
import de.fuberlin.processing.who.cancer.data.Database;
import de.fuberlin.processing.who.cancer.data.structs.StructCancerIncidenceOfLocation;
import de.fuberlin.processing.who.cancer.data.structs.StructCancerMortalityOfLocation;
import de.fuberlin.processing.who.cancer.data.structs.StructCancerAgeSpreadingOfLocation;
import de.fuberlin.processing.who.cancer.utils.Test;


public abstract class CancerCauseAgeBarChart extends BarChart {
	
	private static final int COLOR_CHILD_AGE = ControlP5.papplet.color( 5, 166, 164);
	private static final int COLOR_MID_AGE = ControlP5.papplet.color( 242, 198, 56);
	private static final int COLOR_GRAND_AGE = ControlP5.papplet.color( 75, 0, 70);

	private Vector<StructCancerAgeSpreadingOfLocation> data;
	
	public CancerCauseAgeBarChart( ControlP5 theControlP5,
			ControllerGroup theParent, String theName, float theX, float theY,
			int theWidth, int theHeight) {
		super( theControlP5, theParent, theName, theX, theY, theWidth, theHeight);
		this.setTitle( "age spreading");
	}
	
	
	
	protected void updateScala() {
		if ( isLeftOriented) {
			setLeftScala( "0-14");
			setMidScala( "15-59");
			setRightScala( String.format( "60 +"));
		} else {
			setRightScala( "0-14");
			setMidScala( "15-59");
			setLeftScala( String.format( "60 +"));
		}
	}
	
	
  protected abstract Collection<StructCancerAgeSpreadingOfLocation> getCancerData();
	
	@Override
	protected void loadData() {
		if ( Test.isUnassigned( data))
			data = new Vector<StructCancerAgeSpreadingOfLocation>(); else
		  data.clear();
		data.addAll( getCancerData());
		super.loadData();
	}

	@Override
	protected void drawCell( PApplet p, int locationId, int cellWidth, int cellHeight) {
		p.pushStyle();
		p.fill( 255, 255, 55);
		float[] ageClasses = null;
		for ( StructCancerAgeSpreadingOfLocation location : data) {
			if ( location.locationId == locationId) {
				ageClasses = location.percentageDistributionPerAgeClasses;
				break;
			}
		}
		if ( Test.isAssigned( ageClasses)) {
			float ageClass1Width = p.map( ageClasses[0], 0, 100, 0, cellWidth);
			float ageClass2Width = p.map( ageClasses[1], 0, 100, 0, cellWidth);
			float ageClass3Width = p.map( ageClasses[2], 0, 100, 0, cellWidth);
			
			if ( isLeftOriented) {
				p.fill( COLOR_CHILD_AGE);
				p.rect( 0,  0, ageClass1Width, cellHeight);
				p.fill( COLOR_MID_AGE);
				p.rect( ageClass1Width,  0, ageClass2Width, cellHeight);
				p.fill( COLOR_GRAND_AGE);
				p.rect( ageClass1Width +ageClass2Width,  0, ageClass3Width, cellHeight);				
			} else {
				p.fill( COLOR_GRAND_AGE);
				p.rect( 0,  0, ageClass3Width, cellHeight);
				p.fill( COLOR_MID_AGE);
				p.rect( ageClass3Width,  0, ageClass2Width, cellHeight);
				p.fill( COLOR_CHILD_AGE);
				p.rect( ageClass3Width +ageClass2Width,  0, ageClass1Width, cellHeight);
			}

		}

		p.popStyle();
	}
	

}
